package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.AssistantSubsidy;
import com.rzj.sys_lab_assistant.entity.Technician;
import com.rzj.sys_lab_assistant.entity.bo.AssistantBO;
import com.rzj.sys_lab_assistant.service.IAssistantService;
import com.rzj.sys_lab_assistant.service.IAssistantSubsidyService;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-07-02 22:10
 */

@RequestMapping("assistantSubsidy")
@Controller
public class AssistantSubsidyController {

    @Autowired
    IAssistantSubsidyService assistantSubsidyService;
    @Autowired
    IAssistantService assistantService;

    ResultsUtils resultsUtils;

    @RequestMapping("toAdd")
    public String toAdd(Model model, HttpSession session) {
        Technician technician = (Technician) session.getAttribute("technician");
        model.addAttribute("assistantList", assistantService.listAssistantsById(technician.getAccount()));
        return "assistantSubsidy/assistantSubsidyAdd";
    }

    @RequestMapping("toEdit/{repairId}")
    public String toEdit(Model model, @PathVariable("repairId") int repairId, HttpSession session) {
        Technician technician = (Technician) session.getAttribute("technician");
        model.addAttribute("assistantList", assistantService.listAssistantsById(technician.getAccount())); // session
        model.addAttribute("repair", assistantSubsidyService.getAssistantSubsidyById(repairId));
        return "assistantSubsidy/assistantSubsidyUpdate";
    }

    @RequestMapping("assistantSubsidyList")
    public String assistantSubsidyList(Model model, HttpSession session) {
        AssistantBO assistant = (AssistantBO) session.getAttribute("assistant");
        model.addAttribute("subsidyList", assistantSubsidyService.assistantSubsidiesList(assistant.getAId())); // session
        return "assistantSubsidy/assistantSubsidyList";
    }

    @RequestMapping("assistantSubsidyByIdList")
    public String assistantSubsidyByIdList(Model model, HttpSession session) {
        Technician technician = (Technician) session.getAttribute("technician");
        model.addAttribute("subsidyList", assistantSubsidyService.assistantSubsidiesByIdList(technician.getAccount())); // session
        return "assistantSubsidy/assistantSubsidyByIdList";
    }

    @RequestMapping("assistantSubsidyAdd")
    @ResponseBody
    public Map<String, String> assistantSubsidyAdd(@RequestParam("aId") String aId,
                                                   @RequestParam("money") String money,
                                                   @RequestParam("semester") String semester,
                                                   @RequestParam("year") String year,
                                                   @RequestParam("month") String month,
                                                   @RequestParam("day") String day) {
        AssistantSubsidy assistantSubsidy = new AssistantSubsidy(0, Integer.valueOf(aId), new BigDecimal(money), semester, year, month, day);

        int flag = assistantSubsidyService.saveAssistantSubsidy(assistantSubsidy);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }
    }

    @RequestMapping("assistantSubsidyEdit")
    @ResponseBody
    public Map<String, String> assistantSubsidyEdit(@RequestParam("id") String id,
                                                    @RequestParam("aId") String aId,
                                                    @RequestParam("money") String money,
                                                    @RequestParam("semester") String semester,
                                                    @RequestParam("year") String year,
                                                    @RequestParam("month") String month,
                                                    @RequestParam("day") String day) {
        AssistantSubsidy assistantSubsidy = new AssistantSubsidy(Integer.valueOf(id), Integer.valueOf(aId), new BigDecimal(money), semester, year, month, day);

        int flag = assistantSubsidyService.updateAssistantSubsidy(assistantSubsidy);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }
    }

    @RequestMapping("assistantSubsidyDel")
    @ResponseBody
    public Map<String, String> assistantSubsidyDel(@RequestParam("id") String id) {

        int flag = assistantSubsidyService.deleteAssistantSubsidy(Integer.valueOf(id));
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }
    }
}
